const app = getApp()
import {login} from '../../service/config/index.js'
import {hostName} from '../../service/config/config.js'
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: defaultAvatarUrl,
    realAvatarUrl:"",
    nickname:"" //昵称双向绑定
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 获取临时头像
   * @param {*} e 
   */
  onChooseAvatar(e) {
    let that = this
    const { avatarUrl } = e.detail 
    this.setData({

      //让图片预览处显示刚刚选择的图片
      avatarUrl
    })

    wx.uploadFile({
      filePath: avatarUrl,
      name: 'files',
      url: hostName + "/api/login/uploadUserAvatar",
      success:function(res){
        let json = JSON.parse(res.data)
        if(json.code == 200){
          
          that.setData({
            realAvatarUrl:json.msg
          })
        }
        else{
          wx.showToast({
            title: '上传出错 请稍后重试',
            icon: 'none',
            duration: 2000
          })
        }
      },
      fail:function(res){
        wx.showToast({
          title: res,
          icon: 'error',
          duration: 2000
        })
      }
    })
  },


  /**授权登录，将API调整后返回的nickName和avatarUrl改成我们自定义选择的头像和昵称*/
   submit(e){
    var that=this
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (proFile) => {
          
          wx.login({
            success: function(res){
              let param = {}
              param.code = res.code
              param.signature = proFile.signature
              param.rawData = proFile.rawData
              param.nickName = that.data.nickname
              param.avatarUrl = that.data.realAvatarUrl

              proFile.userInfo.nickName = that.data.nickname
              proFile.userInfo.avatarUrl = that.data.realAvatarUrl
              
              login(param).then(result=>{
                  if(result.code == 200){
                    app.globalData.userInfo = proFile.userInfo
                    console.log(proFile)
                    wx.setStorageSync("userInfo",proFile.userInfo);
                    wx.setStorageSync("token",result.data['token']);
                    wx.setStorageSync("time",result.data['time']);
                    that.setData({
                      userInfo: proFile.userInfo,
                      hasUserInfo: true
                    })
                    wx.switchTab({
                      url: '/pages/profile/index',
                    })
                  }
              })
            }
          })
        },
        fail: res => {
          console.log("获取用户信息失败", res)
        }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})